require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _order = require('service/order-service.js');
var _mm = require('util/mm.js');
var navSide = require('page/common/nav-side/index.js');
var templateHtml = require('./index.string');

var page = {
    data: {
        orderNumber: _mm.getUrlParam('orderNumber')
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        //初始化左侧菜单
        navSide.init({
            name: 'order-list'
        });
        this.loadDetail();
    },
    bindEvent: function () {
        var _this = this;
        $(document).on('click','.order-cancel',function(){
            if(window.confirm('确定取消该订单？')){
                _order.cancelOrder(_this.data.orderNumber,function(res){
                    _mm.successTips('取消订单成功！');
                    _this.loadDetail();
                },function(errMsg){
                    _mm.errorTips(errMsg);
                });
            }
        });
    },
    loadDetail: function () {
        var _this = this,
            detailHtml = '',
            $content = $('.content');
        _order.getOrderDetail(_this.data.orderNumber, function (res) {
            _this.dataFilter(res);
            detailHtml = _mm.renderHtml(templateHtml,res);
            $content.html(detailHtml);
        }, function (errMsg) {
            $content.html('<p class="err-tip">' + errMsg + '</p>')
        });
    },
    dataFilter : function(data){
        console.log('dataFilter=='+(data.status==10));
        data.needPay = data.status == 10;
        data.isCancelable = data.status == 10;
    }
};
$(function () {
    page.init();
});
